home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / STRINGS.H < prev    next >
C/C++ Source or Header  |  1992-11-21  |  2KB  |  71 lines

  1. // -------- strings.h
  2.  
  3. #ifndef STRINGS_H
  4. #define STRINGS_H
  5.  
  6. #include <string.h>
  7. #include "dflatdef.h"
  8.  
  9. // ============================
  10. // BASIC-like String Class
  11. // ============================
  12. class String    {
  13.     char *sptr;
  14.     int length;
  15.     void putstr(char *s);
  16. public:
  17.     // -------- construct a null string
  18.     String() { sptr = NULL; length = 0; }
  19.     // --- construct with char * initializer
  20.     String(char *s);
  21.     // ------- copy constructor
  22.     String(String& s);
  23.     // -------- construct with a size and fill character
  24.     String(int len, char fill = 0);
  25.     // ------- destructor
  26.     ~String() { delete sptr; }
  27.     // ------ return the length of a string
  28.     int Strlen() { return strlen(sptr); }
  29.     int StrBufLen() { return length; }
  30.     // ------ change the buffer length of a string
  31.     void ChangeLength(unsigned int newlen);
  32.     // ---- substring: right len chars
  33.     String right(int len);
  34.     // ---- substring: left len chars
  35.     String left(int len);
  36.     // ---- substring: middle len chars starting from where
  37.     String mid(int len, int where);
  38.     int FindChar(unsigned char ch);
  39.     // ---------- assignment
  40.     String& operator=(String& s);
  41.     // ---------- conversion to char *
  42.     operator char *() { return sptr; }
  43.     // --- concatenation operator (str1 + str2;)
  44.     String operator+(String& s);
  45.     // --- concatenation operator (str1 += str2;)
  46.     void operator+=(String& s) { *this = *this + s; }
  47.     // --- concatenation operator (str1 + "str")
  48.     String& operator+(char *s) { return *this + String(s); }
  49.     // --- concatenation operator (str1 += "str")
  50.     void operator+=(char *s) { *this = *this + String(s); }
  51.  
  52.     // ------- relational operators
  53.     Bool operator==(String& s)
  54.         { return (Bool) (strcmp(sptr,s.sptr) == 0); }
  55.     Bool operator!=(String& s)
  56.         { return (Bool) (strcmp(sptr,s.sptr) != 0); }
  57.     Bool operator>(String& s)
  58.         { return (Bool) (strcmp(sptr,s.sptr) > 0); }
  59.     Bool operator<(String& s)
  60.         { return (Bool) (strcmp(sptr,s.sptr) < 0); }
  61.     Bool operator<=(String& s)
  62.         { return (Bool) (!(*this > s)); }
  63.     Bool operator>=(String& s)
  64.         { return (Bool) (!(*this < s)); }
  65.     // ------- subscript
  66.     char& operator[](int n) { return sptr[n]; }
  67. };
  68.  
  69. #endif
  70.  
  71.